package experiment; /**
 * Created by lenovo on 2016/4/7.
 */
import java.util.Scanner;

public class Caesar {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] c = new char[100];
        System.out.println("请输入密钥：");   //取得密钥
        int key = scanner.nextInt()%26;
        scanner.nextLine();
        System.out.println("请输入明文：");   //取得明文
        String str=scanner.nextLine();
        char[] m=str.toCharArray();

        if (m.length>=100){
            System.out.println("输入过长，请控制在100英文字符以内");
        }

        for (int i=0;i<m.length;i++){    //加密
            if ((m[i]>='a'&& m[i]<='z')||(m[i]>='A'&& m[i]<='Z')) {
                c[i] = (char) (m[i]+key);
                if (key>0){
                    if ((c[i]<'a'&& c[i]>'Z')||c[i]>'z'){
                        c[i] -= 26;
                    }
                }
                else{
                    if ((c[i]<'a'&& c[i]>'Z')||c[i]<'A'){
                        c[i] += 26;
                    }
                }
            }
            else {
                c[i]=m[i];
            }
        }

        System.out.println("密文：");      //输出密文
        for (int i=0;i<m.length;i++) {
            System.out.printf("%c",c[i]);
        }
    }
}
